package fiszki.namespace;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ListView;

public class LearningActivity extends Activity {
	private ImageButton start;
    private ImageButton back;
    private ListView lvFiszki;
    private int setID;
    
    private FiszkiDBAdapter fiszkiDBAdapter;
    private Cursor fiszkiCursor;
    private List<FiszkaTasks> fiszki;
    private FiszkaTasksAdapter listAdapter;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //setContentView(R.layout.learning);

        initUiElements();
        //initListView();
        //initButtonsOnClickListeners(); 
	}
	
	private void initUiElements() { 	
        //start = (ImageButton) findViewById(R.id.buttonStartLearning);
        back = (ImageButton) findViewById(R.id.buttonBack);
        lvFiszki = (ListView) findViewById(R.id.lvFiszki);
    	setID = getIntent().getExtras().getInt("setID");
    }
    
    private void initButtonsOnClickListeners() { 	 	
    	start.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	Bundle ID = new Bundle();
            	ID.putInt("setID", setID);
            	Intent newIntent = new Intent(LearningActivity.this, FiszkiSystemActivity.class);
            	newIntent.putExtras(ID);
            	startActivityForResult(newIntent, 0);
            }
        });
    	
    	back.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	finish();
            }
        });
	} 
    
    private void initListView() {
    	fiszkiDBAdapter = new FiszkiDBAdapter(getApplicationContext());
    	fiszkiDBAdapter.open();
        getAllFiszki();
        listAdapter = new FiszkaTasksAdapter(this, fiszki);
        listAdapter.notifyDataSetChanged();
        lvFiszki.setAdapter(listAdapter);
        registerForContextMenu(lvFiszki);
    }
    
    private void getAllFiszki() {
        fiszki = new ArrayList<FiszkaTasks>();
        fiszkiCursor = getAllEntriesFromDB();
        
        if(fiszkiCursor != null && fiszkiCursor.moveToFirst()) {
            do {
                int id = fiszkiCursor.getInt(FiszkiDBAdapter.F_ID_COLUMN);
                int setID = fiszkiCursor.getInt(FiszkiDBAdapter.SET_COLUMN);
                String question = fiszkiCursor.getString(FiszkiDBAdapter.QUESTION_COLUMN);
                String answer = fiszkiCursor.getString(FiszkiDBAdapter.ANSWER_COLUMN);
                
                fiszki.add(new FiszkaTasks(id, setID, question, answer));
            } 
            while(fiszkiCursor.moveToNext());
        }
    }
     
    // OK
    private Cursor getAllEntriesFromDB() {
        fiszkiCursor = fiszkiDBAdapter.getAllFiszki(setID);
        
        if(fiszkiCursor != null) {
            startManagingCursor(fiszkiCursor);
            fiszkiCursor.moveToFirst();
        }
        
        return fiszkiCursor;
    }
    
    @Override
    protected void onDestroy() {
        if(fiszkiDBAdapter != null)
        	fiszkiDBAdapter.close();
        
        super.onDestroy();
    }	    
}
